<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {

    public function index(){
    	
    	//查找出主页的置顶和精贴的 问题数据 3条
    	$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where badge<>'0' order by create_time desc limit 3";
    	//$sql = "select * from question where badge='0' ";
    	$badge = M()->query($sql); //$badge[标记、徽章]
    	//var_dump($badge);
    	$this->assign('badge',$badge);


    	//分页部分 counts 帖子总数 分配到模板 分页使用
    	$sql = "select count(id) from question where badge='0'";
    	$counts = current(M()->query($sql));
    	//var_dump($counts['count(id)']);
    	$this->assign('Qcounts',$counts['count(id)']);

    	//分页部分 分页展示 问题数据
    	$pageCurr = I('get.page');  //当前页数
    	$pageLimit = 5; //限定取多少条数据[该数值会影响生成的页码]
    	$this->assign('pageLimit',$pageLimit);
    	//var_dump($pageCurr,$pageLimit);
    	$startNum = ($pageCurr-1)*$pageLimit; //$startNum 从第几条数据开始取
    	//var_dump($startNum);
    	if (empty($pageCurr) || $pageCurr<1 ) {
    		$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where badge='0' order by create_time desc limit 0,25";
    	}else{
    		$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where badge='0' order by create_time desc limit ".$startNum.",".$pageLimit;
    	}
    	//查找出主页的普通(状态为未结贴)贴的 问题数据 25条
    	//$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where badge='0' order by create_time desc limit 25";
    	$ordinary = M()->query($sql); //$ordinary[普通]
    	//var_dump($ordinary);
    	$this->assign('ordinary',$ordinary);


    	//调用公共控制器 获取帖子右部分的 回答月榜 用户数据展示
    	$this->getTopUser();

    	//调用公共控制器 获取帖子右部分的 近期热议 数据展示
    	$this->getTopAnswer();

    	//分配网络标题
    	$this->assign('title','Fly - layui前端框架官方社区');
        $this->display();



    }

    //主页的分页  处理异步分页
    public function ajaxPage()
    {	
    	$pageCurr = I('post.pageCurr');
    	$pageLimit = I('post.pageLimit');// 0  3  D1   3  3  D2   6  3  D3   D-1*L   1-1*3=0  2-1*3=3  3-1*3=6  
    	$startNum = ($pageCurr-1)*$pageLimit;
    	// var_dump($pageCurr,$startNum);
    	$arr = array('error'=>0,'curr'=>$pageCurr,'limit'=>$pageLimit,);
    		echo json_encode($arr);
    }
}